package com.hue.springbootneo4j.service.impl;

import com.hue.springbootneo4j.entity.Star;
import com.hue.springbootneo4j.entity.StarRelationShip;
import com.hue.springbootneo4j.repository.StarRelationShipRepository;
import com.hue.springbootneo4j.service.StarRelationShipService;
import org.neo4j.ogm.response.model.RelationshipModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StarRelationShipServiceImpl implements StarRelationShipService {
    @Autowired
    private StarRelationShipRepository starRelationShipRepository;

    @Override
    public List<StarRelationShip> all() {
        List<Map<String, RelationshipModel>> all = starRelationShipRepository.all();
        List<StarRelationShip> starRelationShipList = all.stream().map(map -> {
            StarRelationShip starRelationShip = new StarRelationShip();
            RelationshipModel model = map.get("r");
            starRelationShip.setId(model.getId());
            starRelationShip.setStart(new Star(model.getStartNode()));
            starRelationShip.setEnd(new Star(model.getEndNode()));
            starRelationShip.setRelation(model.getType());
            return starRelationShip;
        }).collect(Collectors.toList());
        return starRelationShipList;
    }
}
